

import 'dart:io';

import 'package:bt_tool/basic/bt_file_tool.dart';
import 'package:bt_tool/basic/bt_model.dart';
import 'package:bt_tool/widget/bt_toast.dart';
import 'package:flutter/services.dart';

class ImgPathModel extends BTModel{

  int id = 0;
  String resultPath = "";
  String imgPath_1 = "";
  String imgPath_2 = "";
  String imgName = "";
  int time = 0;
  String flutterConfigStr = "";
  String extension = "";
  int isIosStyle = 0;

  ImgPathModel(super.oriJsonData);

  @override
  parse(){
    super.parse();
    id = jsonTool.getInt("id");
    resultPath = jsonTool.getString("resultPath");
    imgPath_1 = jsonTool.getString("imgPath_1");
    imgPath_2 = jsonTool.getString("imgPath_2");
    imgName = jsonTool.getString("imgName");
    time = jsonTool.getInt("time");
    flutterConfigStr = jsonTool.getString("flutterConfigStr");
    extension = jsonTool.getString("extension");
    isIosStyle = jsonTool.getInt("isIosStyle");
  }

  @override
  Map<String, dynamic> convertToDict() {
    var map = super.convertToDict();
    map["id"] = id;
    map["resultPath"] = resultPath;
    map["imgPath_1"] = imgPath_1;
    map["imgPath_2"] = imgPath_2;
    map["imgName"] = imgName;
    map["time"] = time;
    map["flutterConfigStr"] = flutterConfigStr;
    map["extension"] = extension;
    map["isIosStyle"] = isIosStyle;
    return map;
  }

  Future<void> saveImgIos() async {
    if(isIosStyle == btFieldTrue){
      String scale1ResultPath = "$resultPath/$imgName$extension";
      String scale3ResultPath = "$resultPath/3.0x/$imgName$extension";

      if(await File(imgPath_1).exists() == false || await File(imgPath_2).exists() == false){
        BTToast.showToast("源数据丢失");
        return;
      }


      try{
        await BTFileTool.copyFile(imgPath_1, scale1ResultPath);
        if(await Directory("$resultPath/2.0x").exists()){
          await BTFileTool.copyFile(imgPath_1, "$resultPath/2.0x/$imgName$extension");
        }

        if(await Directory("$resultPath/3.0x").exists()){
          await BTFileTool.copyFile(imgPath_2, "$resultPath/3.0x/$imgName$extension");
        }

        BTToast.showToast("操作成功");
        // Clipboard.setData(ClipboardData(text: flutterConfigStr));
      }catch (e){
        BTToast.showToast(e.toString());
      }
      return;
    }

    String path = "$resultPath/$imgName$extension";
    if(await File(imgPath_1).exists() == false){
      BTToast.showToast("源数据丢失");
      return;
    }
    try{
      await BTFileTool.copyFile(imgPath_1, path);
      BTToast.showToast("操作成功,配置信息已复制到粘贴板");
      Clipboard.setData(ClipboardData(text: flutterConfigStr));
    }catch (e){
      BTToast.showToast(e.toString());
    }

  }

  deleteFile() async {
    if(isIosStyle == btFieldTrue){
      String scale1ResultPath = "$resultPath/$imgName$extension";
      String scale2ResultPath = "$resultPath/2.0x/$imgName$extension";
      String scale3ResultPath = "$resultPath/3.0x/$imgName$extension";
      try{
        await BTFileTool.deleteFile(File(scale1ResultPath));
        await BTFileTool.deleteFile(File(scale2ResultPath));
        await BTFileTool.deleteFile(File(scale3ResultPath));
        BTToast.showToast("删除成功");
      }catch(e){
        BTToast.showToast(e.toString());
      }
      return;
    }

    String firstResultPath = "$resultPath/$imgName$extension";
    try{
      await BTFileTool.deleteFile(File(firstResultPath));
      BTToast.showToast("删除成功");
    }catch(e){
      BTToast.showToast(e.toString());
    }
  }

}